﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Threading;
using System.IO;
using Vit.Common.Helpers;
using Vit.Web.Helpers;

namespace Vit.Web.Mvc.Controllers
{
    public class LocalizationController : Controller
    {
        [HttpGet]
        public ActionResult CurrentCulture()
        {
            string culture = Thread.CurrentThread.CurrentCulture.Name;
            return Json(culture, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [OutputCache(Duration = 3600, VaryByParam = "id", VaryByCustom = "session")]
        public ActionResult GetLocalScript(string id)
        {
            id = id.Replace('.', '_');
            string culture = Thread.CurrentThread.CurrentCulture.Name;
            string filePath = Server.MapPath(string.Format("/Localizations/{0}.{1}.js", id, culture));
            if (!System.IO.File.Exists(filePath))
            {
                filePath = Server.MapPath(string.Format("/Localizations/{0}.js", id));
            }
            return File(filePath, " text/plain");
        }
    }
}
